package stream;

import lambda.Employee;

import java.util.*;
import java.util.stream.Collectors;

public class StreamAPI3 {
    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
                new Employee("张三",35,4000.0, Employee.Status.BUSY),
                new Employee("李四",34,3500.0, Employee.Status.FREE),
                new Employee("王五",25,3000.0, Employee.Status.VACATION),
                new Employee("赵六",37,3100.0, Employee.Status.BUSY),
                new Employee("田七",34,3300.0, Employee.Status.VACATION)
        );

        System.out.println(employees.stream().allMatch((e) -> e.getStatus().equals(Employee.Status.BUSY)));
        System.out.println(employees.stream().anyMatch((e) -> e.getStatus().equals(Employee.Status.BUSY)));
        System.out.println(employees.stream().noneMatch((e) -> e.getStatus().equals(Employee.Status.BUSY)));
        Optional<Employee> optionalEmployee = employees.stream().sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())).findFirst();
        System.out.println(optionalEmployee.get());
        Optional<Employee> employee = employees.stream().filter((e) -> e.getStatus().equals(Employee.Status.FREE)).findAny();
        System.out.println(employee.get());
        System.out.println(employees.stream().count());
        System.out.println(employees.stream().max(Comparator.comparingDouble(Employee::getSalary)));

        System.out.println("--------------");

        /**
         * 归约
         * reduce
         */
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8);
        System.out.println(list.stream().reduce(0, (x, y) -> x + y));

        System.out.println(employees.stream().map(Employee::getSalary).reduce(Double::sum));

        List<String> collect = employees.stream().map(Employee::getName).collect(Collectors.toList());
        collect.forEach(System.out::println);

        Set<String> set = employees.stream().map(Employee::getName).collect(Collectors.toSet());
        set.forEach(System.out::println);

        HashSet<String> hashSet = employees.stream().map(Employee::getName).collect(Collectors.toCollection(HashSet::new));
        hashSet.forEach(System.out::println);

        System.out.println(employees.stream().collect(Collectors.counting()));
        System.out.println(employees.stream().count());

        System.out.println(employees.stream().collect(Collectors.averagingDouble(Employee::getSalary)));

        Map<Employee.Status, List<Employee>> listMap = employees.stream().collect(Collectors.groupingBy(Employee::getStatus));
        System.out.println(listMap);

        Map<Boolean, List<Employee>> map = employees.stream().collect(Collectors.partitioningBy((e) -> e.getSalary() > 3300));
        System.out.println(map);

        System.out.println(employees.stream().map(Employee::getName).collect(Collectors.joining(",", "===", "===")));
    }
}
